Crate ohms

source ·
Expand description

Electrical unit types for embedded targets focusing on ease-of-use and performance

Supported units:

  • Current (μA, mA, A)
  • Resistance (mΩ, Ω, kΩ, MΩ)
  • Voltage (μV, mV, V, kV)

Each unit type has a corresponding extension trait for creating values from i32/u32 and f32 values. The extension traits are named ExtI32, ExtU32, and ExtF32 respectively.

Unit types can easily be converted to and from different denominations.

Ohm’s Law is implemented for Voltage and Current types, allowing you to easily calculate between the three units using the / and * operators.

Examples

Determine the resistance of a 5V, 1A load:

use ohms::prelude::*;

let voltage = 5.volts();
let current = 1u32.amps();

let resistance = voltage / current; // 5V / 1A = 5Ω
println!("Resistance: {} Ω", resistance.ohms());

Determine the current of a 5V, 220Ω load:

use ohms::prelude::*;

let voltage = 5.volts();
let resistance = 220.ohms();

let current = voltage / resistance; // 5V / 220Ω = 22.72mA
println!("Current: {} mA", current.milli_amps());

Determine the voltage of a 0.5A, 4.7kΩ load:

use ohms::prelude::*;

let current = 0.5f32.milli_amps();
let resistance = 4.7f32.kilo_ohms();

let voltage = current * resistance; // 0.5mA * 4.7kΩ = 2.35V
println!("Voltage: {} V", voltage.volts());

Modules

Structs

Represents a current value, stored as whole microamps (μA). This value can only be positive.
Represents a resistance value, stored as whole milliohms (mΩ). This value can only be positive.
Represents a voltage value, stored as whole microvolts (μV). This value can be positive or negative.

Traits

Extension trait for simple short-hands for creating Current values from f32 values.
Extension trait for simple short-hands for creating Current values from u32 values.
Extension trait for simple short-hands for creating Resistance values from f32 values.
Extension trait for simple short-hands for creating Resistance values from u32 values.
Extension trait for simple short-hands for creating Voltage values from f32 values.
Extension trait for simple short-hands for creating Voltage values from i32 values.